{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建DataFrame对象\n",
    "import pandas as pd\n",
    "\n",
    "employee = {\n",
    "    \"name\": [\"Li Yilun\", \"Zhong Guolin\", \"Fu Yong\", \"Sen Xiaomei\"],\n",
    "    \"gender\": [\"male\", \"male\", \"male\", \"female\"],\n",
    "    \"email\": [\"lyl@ynxh.com\", \"zgl@ynxh.com\", \"fy@ynxh.com\", \"sxm@ynxh.com\"]\n",
    "}\n",
    "\n",
    "ds = pd.DataFrame(employee)\n",
    "\n",
    "print(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建Series对象\n",
    "import pandas as pd\n",
    "\n",
    "l = [1, 2, 3, 4, 5]\n",
    "\n",
    "myseries = pd.Series(l, dtype=int, name=\"dd\", copy=False)\n",
    "print(myseries)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建Series对象\n",
    "import pandas as pd\n",
    "\n",
    "l = [1, 2, 3, 4, 5]\n",
    "\n",
    "myseries = pd.Series(l, index=[7, 8, 9, 10, 11], dtype=int, name=\"dd\", copy=False)\n",
    "print(myseries)\n",
    "print(myseries[11])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建DATa Frame\n",
    "import pandas as pd\n",
    "\n",
    "lst = [[\"Python Programming\", 10.99], [\"Thinking in Java\", 19.99]]\n",
    "cols = [\"Title\", \"Price\"]\n",
    "idx = [1, 2]\n",
    "\n",
    "df = pd.DataFrame(data=lst, columns=cols, index=idx, copy=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基于字典创建Data Frame\n",
    "import pandas as pd\n",
    "\n",
    "dataSet = {\n",
    "    \"Title\": [\"Python Programming\", \"Thinking in Java\"],\n",
    "    \"Price\": [9.99, 19.99]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(dataSet)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 还是使用字典创建DATa Frame\n",
    "import pandas as pd\n",
    "\n",
    "datSet = [\n",
    "    {\n",
    "        \"Title\": \"Python Programming\",\n",
    "        \"Price\": 9.99\n",
    "    },\n",
    "    {\n",
    "        \"Title\": \"Thinking in Java\",\n",
    "        \"Price\": 19.99\n",
    "    }\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(datSet)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典创建数据框架\n",
    "import pandas as pd\n",
    "\n",
    "data = [\n",
    "    {\n",
    "        \"a\": 1,\n",
    "        \"b\": 2\n",
    "    },\n",
    "    {\n",
    "        \"a\": 3,\n",
    "        \"b\": 4,\n",
    "        \"c\": 5\n",
    "    }\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# loc 列到数据\n",
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    \"name\": [\"李易伦\", \"钟国林\", \"付勇\"],\n",
    "    \"基本工资\": [1200, 1100, 1200],\n",
    "    \"加班工资\": [300, 360, 300]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "print(df)\n",
    "print(df.loc[0:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pandas and csv\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"vgsales.csv\")\n",
    "print(df.to_string())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pandas and csv\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"vgsales.csv\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save data frame to csv, excel\n",
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    \"name\": [\"李易伦\", \"钟国林\", \"付勇\"],\n",
    "    \"基本工资\": [1200, 1100, 1200],\n",
    "    \"加班工资\": [300, 360, 300]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "df.to_csv(\"emp.csv\")\n",
    "df.to_excel(\"emp.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# read top 10\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"vgsales.csv\")\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# read tail(5)\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"vgsales.csv\")\n",
    "print(df.tail(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get df info\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"vgsales.csv\")\n",
    "print(df.info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pandas with JSON\n",
    "\n",
    "Pandas 可以方便的处理Json数据，请参看下面的操作及代码：\n",
    "\n",
    "* 创建```emp.json```文件并包括以下内容\n",
    "\n",
    "```json\n",
    "[\n",
    "    {\n",
    "        \"name\": \"Liyilun\",\n",
    "        \"gender\": \"male\",\n",
    "        \"salary\": 2100\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Zhongguolin\",\n",
    "        \"gender\": \"male\",\n",
    "        \"salary\": 2000\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Fuyong\",\n",
    "        \"gender\": \"male\",\n",
    "        \"salary\": 2200\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Jinzd\",\n",
    "        \"gender\": \"male\",\n",
    "        \"salary\": 1800\n",
    "    }\n",
    "]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_json(\"emp.json\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* https://www.runoob.com/pandas/pandas-json.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "one      1\n",
      "two      2\n",
      "three    3\n",
      "dtype: int64\n",
      "Index(['one', 'two', 'three'], dtype='object')\n",
      "[1 2 3]\n",
      "0     2\n",
      "1     3\n",
      "2     4\n",
      "3     5\n",
      "4     6\n",
      "5     7\n",
      "6     8\n",
      "7     9\n",
      "8    10\n",
      "9    11\n",
      "dtype: int32\n",
      "1    3\n",
      "dtype: int32\n",
      "3    5\n",
      "5    7\n",
      "7    9\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "s = pd.Series(data=[1, 2, 3], index=[\"one\",\"two\", \"three\"])\n",
    "print(s[\"two\"])\n",
    "print(s[0])\n",
    "print(s)\n",
    "print(s.index)\n",
    "print(s.values)\n",
    "\n",
    "s = pd.Series(np.arange(10))\n",
    "s += 2\n",
    "print(s)\n",
    "\n",
    "print(s[1:2])\n",
    "\n",
    "print(s[[3, 5, 7]])"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d80dceb8e76061e928794bb3ce8fa9d0c2b7abe9c550ab61d3fca5a520d3bcb0"
  },
  "kernelspec": {
   "display_name": "Python 3.8.6 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
